function TodoListAction(action,key,txtdepartment){
   ShowSpinner();
   if(action=='save'){
     done=0;
   }else if(action=='currentview'){
     action=$("#view").val();
     done=0;
   }else{
     done=1;
   }
   var title=$('#title').val();
   $('#title').val('');
   var department=$('#department option:selected').val();
   var description=$('#description').val();
   $('#description').val('');
   if(action=='save'){
     var fecha_actual = new Date();  
     var dia = fecha_actual.getDate();
     var mes = fecha_actual.getMonth() + 1; 
     var anio = fecha_actual.getFullYear();
     var hora = fecha_actual.getHours();
     var minutos = fecha_actual.getMinutes();
     var todoitem = $('<div id="tmptodo" class="taskactions '+department.toLowerCase()+'"></div>');
     htmltext='<div class="task"><p class="todotit"><strong>'+title+'</strong></p><p>'+dia+'/'+mes+'/'+anio+' '+hora+':'+minutos+'- <strong class="department">'+department+'</strong> - Added by unknown';
     htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+key+'\',\'\'); return false;">Remove</a> - <a href="javascript:" onclick="TodoListAction(\'lock\',\''+key+'\',\'\'); return false;">Lock</a></p><p>'+description+'</p></div>';
     htmltext+='<p class="done"><a href="#" onclick="TodoListAction(\'done\',\''+key+'\',\'\'); return false;">Done</a></p>';
     todoitem.html(htmltext);
     todoitem.hide();
     $('#todosbox').prepend(todoitem);
     todoitem.fadeIn(500);
   }
   $.post('/admin/todolist/',
    { title: title, description: description, department: department, done: done, action: action, key: key, txtdepartment: txtdepartment },
    function(data){
      HideSpinner();
      data=eval(data);
     if(data.length>0){
      if(data[0].status=='save'){
            $('#tmptodo').remove();
            var todoitem = $('<div id="'+data[0].key+'"></div>');
            htmltext='<div class="task"><p class="todotit"><strong>'+data[0].title+'</strong></p><p>'+data[0].pub_date+' - <strong class="department">'+data[0].department+'</strong> - Added by '+data[0].creator;
            if(data[0].locker!=''){
              if(data[0].locker==data[0].username || data[0].creator==data[0].username || data[0].username=='guiademuseos'){
                htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[0].key+'\',\'\'); return false;">Remove</a> - Locked by <strong>'+data[0].locker+'</strong> <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[0].key+'\',\'\'); return false;">UNLOCK</a></p><p>'+data[0].description+'</p></div>';
                htmltext+='<p class="done lockedbyuser"><a href="#" onclick="TodoListAction(\'done\',\''+data[0].key+'\',\'\'); return false;">Done</a></p>';
              }else{
                htmltext+='- Locked by <strong>'+data[0].locker+'</strong></p><p>'+data[0].description+'</p></div>';
                htmltext+='<p class="locked"><img src="/img/lock.png" /></p>';
              }
            }else{
              htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[0].key+'\',\'\'); return false;">Remove</a> - <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[0].key+'\',\'\'); return false;">Lock</a></p><p>'+data[0].description+'</p></div>';
              htmltext+='<p class="done"><a href="#" onclick="TodoListAction(\'done\',\''+data[0].key+'\',\'\'); return false;">Done</a></p>';
            }
            todoitem.html(htmltext);
            todoitem.hide();
            todoitem.attr('class','taskactions '+data[0].department.toLowerCase());
            $('#todosbox').prepend(todoitem);
            todoitem.fadeIn(500);
      }else if(data[0].status=='notsave'){
        alert("title cannot be empty");
      }else if(data[0].status=='lock'){
            var todoitem = $('#'+data[0].key);
            htmltext='<div class="task"><p class="todotit"><strong>'+data[0].title+'</strong></p><p>'+data[0].pub_date+' - <strong class="department">'+data[0].department+'</strong> - Added by '+data[0].creator;
            if(data[0].locker!=''){
              if(data[0].locker==data[0].username || data[0].creator==data[0].username || data[0].username=='guiademuseos'){
                htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[0].key+'\',\'\'); return false;">Remove</a> - Locked by <strong>'+data[0].locker+'</strong> <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[0].key+'\',\'\'); return false;">UNLOCK</a></p><p>'+data[0].description+'</p></div>';
                htmltext+='<p class="done lockedbyuser"><a href="#" onclick="TodoListAction(\'done\',\''+data[0].key+'\',\'\'); return false;">Done</a></p>';
              }else{
                htmltext+='- Locked by <strong>'+data[0].locker+'</strong></p><p>'+data[0].description+'</p></div>';
                htmltext+='<p class="locked"><img src="/img/lock.png" /></p>';
              }
            }else{
              htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[0].key+'\',\'\'); return false;">Remove</a> - <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[0].key+'\',\'\'); return false;">Lock</a></p><p>'+data[0].description+'</p></div>';
              htmltext+='<p class="done"><a href="#" onclick="TodoListAction(\'done\',\''+data[0].key+'\',\'\'); return false;">Done</a></p>';
            }
            todoitem.html(htmltext);
      }else if(data[0].status=='done'){
          todoitem = $("#"+data[0].key);
          todoitem.fadeOut(1000);
      }else if(data[0].status=='remove'){
          todoitem = $("#"+data[0].key);
          todoitem.fadeOut(1000);
      }else if(data[0].status=="initcounters"){
          $('#pendingscounter').html(data[0].counterpendings);
          $('#donescounter').html(data[0].counterdones);
      }else if(data[0].status=='loaddone'){
          title = $("#todolisth2");
          $("#view").val('loaddone');
          if(txtdepartment==''){
            $('#donescounter').html(''+data.length);
            txtdepartment='done';
            designnum=0;
            developmentnum=0;
            contentnum=0;
            strategynum=0;
            frontendnum=0;
            othernum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','active');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
            title.html('All Done todos');
          }else if(txtdepartment=='Development'){
            title.html('Done todos of '+txtdepartment);
            developmentnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','active');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Design'){
            title.html('Done todos of '+txtdepartment);
            designnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','active');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Content'){
            title.html('Done todos of '+txtdepartment);
            contentnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','active');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Strategy'){
            title.html('Done todos of '+txtdepartment);
            strategynum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','active');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Frontend'){
            title.html('Done todos of '+txtdepartment);
            frontendnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','active');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Other'){
            title.html('Done todos of '+txtdepartment);
            othernum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','active');
          }
          todosbox = $("#todosbox");
          todosbox.html('');
          for(i=0;i<data.length;i++){
            var todoitem = $('<div id="'+data[i].key+'"></div>');
            todoitem.html('<div class="task"><p class="todotit"><strong>'+data[i].title+'</strong></p><p>'+data[i].pub_date+' - <strong class="department">'+data[i].department+'</strong> - Added by '+data[i].creator+' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[i].key+'\',\'\'); return false;">Remove</a></p><p>'+data[i].description+'</p></div>');
            $('#todosbox').append(todoitem);
            $("#"+data[i].key).hide();
            $("#"+data[i].key).fadeIn(500);
            if(data[i].department=='Development'){
              developmentnum++;
              $("#"+data[i].key).attr('class','taskactions development');
            }else if(data[i].department=='Design'){
              designnum++;
              $("#"+data[i].key).attr('class','taskactions design');
            }else if(data[i].department=='Strategy'){
              strategynum++;
              $("#"+data[i].key).attr('class','taskactions strategy');
            }else if(data[i].department=='Content'){
              contentnum++;
              $("#"+data[i].key).attr('class','taskactions content');
            }else if(data[i].department=='Frontend'){
              frontendnum++;
              $("#"+data[i].key).attr('class','taskactions frontend');
            }else if(data[i].department=='Other'){
              frontendnum++;
              $("#"+data[i].key).attr('class','taskactions other');
            }
          }
          $('#developmentcounter').html(''+developmentnum);
          $('#designcounter').html(''+designnum);
          $('#contentcounter').html(''+contentnum);
          $('#strategycounter').html(''+strategynum);
          $('#frontendcounter').html(''+frontendnum);
          $('#othercounter').html(''+othernum);
        }else if(data[0].status=='loadpending'){
          title = $("#todolisth2");
          $("#view").val('loadpending');
          if(txtdepartment==''){
            $('#pendingscounter').html(''+data.length);
            txtdepartment='pending';
            designnum=0;
            developmentnum=0;
            contentnum=0;
            strategynum=0;
            frontendnum=0;
            othernum=0;
            $('#pendingslink').attr('class','active');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
            title.html('All pending todos');
          }else if(txtdepartment=='Development'){
            title.html('Pendings todos of '+txtdepartment);
            developmentnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','active');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Design'){
            title.html('Pendings todos of '+txtdepartment);
            designnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','active');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Content'){
            title.html('Pendings todos of '+txtdepartment);
            contentnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','active');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Strategy'){
            title.html('Pendings todos of '+txtdepartment);
            strategynum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','active');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Frontend'){
            title.html('Pendings todos of '+txtdepartment);
            frontendnum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','active');
            $('#otherlink').attr('class','');
          }else if(txtdepartment=='Other'){
            title.html('Pendings todos of '+txtdepartment);
            othernum=0;
            $('#pendingslink').attr('class','');
            $('#doneslink').attr('class','');
            $('#designlink').attr('class','');
            $('#developmentlink').attr('class','');
            $('#contentlink').attr('class','');
            $('#strategylink').attr('class','');
            $('#frontendlink').attr('class','');
            $('#otherlink').attr('class','active');
          }
          todosbox = $("#todosbox");
          todosbox.html('');
          for(i=0;i<data.length;i++){
            var todoitem = $('<div id="'+data[i].key+'"></div>');
            htmltext='<div class="task"><p class="todotit"><strong>'+data[i].title+'</strong></p><p>'+data[i].pub_date+' - <strong class="department">'+data[i].department+'</strong> - Added by '+data[i].creator;
            if(data[i].locker!=''){
              if(data[i].locker==data[i].username || data[i].creator==data[i].username || data[i].username=='guiademuseos'){
                htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[i].key+'\',\'\'); return false;">Remove</a> - Locked by <strong>'+data[i].locker+'</strong> <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[i].key+'\',\'\'); return false;">UNLOCK</a></p><p>'+data[i].description+'</p></div>';
                htmltext+='<p class="done lockedbyuser"><a href="#" onclick="TodoListAction(\'done\',\''+data[i].key+'\',\'\'); return false;">Done</a></p>';
              }else{
                htmltext+='- Locked by <strong>'+data[i].locker+'</strong></p><p>'+data[i].description+'</p></div>';
                htmltext+='<p class="locked"><img src="/img/lock.png" /></p>';
              }
            }else{
              htmltext+=' - <a href="#" onclick="TodoListAction(\'remove\',\''+data[i].key+'\',\'\'); return false;">Remove</a> - <a href="javascript:" onclick="TodoListAction(\'lock\',\''+data[i].key+'\',\'\'); return false;">Lock</a></p><p>'+data[i].description+'</p></div>';
              htmltext+='<p class="done"><a href="#" onclick="TodoListAction(\'done\',\''+data[i].key+'\',\'\'); return false;">Done</a></p>';
            }
            todoitem.html(htmltext);
            $('#todosbox').append(todoitem);
            $("#"+data[i].key).hide();
            $("#"+data[i].key).fadeIn(500);
            if(data[i].department=='Development'){
              developmentnum++;
              $("#"+data[i].key).attr('class','taskactions development');
            }else if(data[i].department=='Design'){
              designnum++;
              $("#"+data[i].key).attr('class','taskactions design');
            }else if(data[i].department=='Strategy'){
              strategynum++;
              $("#"+data[i].key).attr('class','taskactions strategy');
            }else if(data[i].department=='Content'){
              contentnum++;
              $("#"+data[i].key).attr('class','taskactions content');
            }else if(data[i].department=='Frontend'){
              frontendnum++;
              $("#"+data[i].key).attr('class','taskactions frontend');
            }else if(data[i].department=='Other'){
              othernum++;
              $("#"+data[i].key).attr('class','taskactions other');
            }
          }
          $('#developmentcounter').html(''+developmentnum);
          $('#designcounter').html(''+designnum);
          $('#contentcounter').html(''+contentnum);
          $('#strategycounter').html(''+strategynum);
          $('#frontendcounter').html(''+frontendnum);
          $('#othercounter').html(''+othernum);
        }
      }else{
        title = $("#todolisth2");
        if(action=='loadpending'){
          if(txtdepartment==''){
            title.html('All pendings todos');
          }else if(txtdepartment=='Development'){
            title.html('Pending todos of '+txtdepartment);
          }else if(txtdepartment=='Design'){
            title.html('Pending todos of '+txtdepartment);
          }else if(txtdepartment=='Content'){
            title.html('Pending todos of '+txtdepartment);
          }else if(txtdepartment=='Strategy'){
            title.html('Pending todos of '+txtdepartment);
          }else if(txtdepartment=='Frontend'){
            title.html('Pending todos of '+txtdepartment);
          }else if(txtdepartment=='Other'){
            title.html('Pending todos of '+txtdepartment);
          }
        }else if(action=='loaddone'){
          if(txtdepartment==''){
            title.html('All Done todos');
          }else if(txtdepartment=='Development'){
            title.html('Todos of '+txtdepartment+' dones');
          }else if(txtdepartment=='Design'){
            title.html('Todos of '+txtdepartment+' dones');
          }else if(txtdepartment=='Content'){
            title.html('Todos of '+txtdepartment+' dones');
          }else if(txtdepartment=='Strategy'){
            title.html('Todos of '+txtdepartment+' dones');
          }else if(txtdepartment=='Frontend'){
            title.html('Todos of '+txtdepartment+' dones');
          }else if(txtdepartment=='Other'){
            title.html('Todos of '+txtdepartment+' dones');
          }
        }
        todosbox = $("#todosbox");
        todosbox.html('');
        var todoitem = $('<div id="empty"></div>');
        todoitem.html('<div class="task"><p class="todotit"><strong>There is not any task for this department :)</p></div>');
        $('#todosbox').append(todoitem);
      }
    });
}

function ShowSpinner(){
  if(!hasspinner){
    hasspinner = true;
    htmlspinner='<span id="spinner"><img src="/img/ajax-loader.gif" /></span>';
    spinnercontainer=$('#spinner-container');
    spinnercontainer.prepend(htmlspinner);
  }
}

function HideSpinner(){
  if(hasspinner){
    hasspinner = false;
    $('#spinner').remove();
  }
}
